home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
ada
/
c01lab4.zip
/
LRMRDR
/
LRM_CHAP.ZIP
/
CHAP07.DOC
< prev
next >
Wrap
Text File
|
1992-04-21
|
37KB
|
887 lines
> 7. Packages
Packages are one of the four forms of program unit, of which programs can
be composed. The other forms are subprograms, task units, and generic
units.
Packages allow the specification of groups of logically related entities.
In their simplest form packages specify pools of common object and type
declarations. More generally, packages can be used to specify groups of
related entities including also subprograms that can be called from outside
the package, while their inner workings remain concealed and protected from
outside users.
References: generic unit 12, program unit 6, subprogram 6, task unit 9,
type declaration 3.3.1
> 7.1 Package Structure
A package is generally provided in two parts: a package specification and
a package body. Every package has a package specification, but not all
packages have a package body.
package_declaration ::= package_specification;
package_specification ::=
package identifier is
{basic_declarative_item}
[private
{basic_declarative_item}]
end [package_simple_name]
package_body ::=
package body package_simple_name is
[declarative_part]
[begin
sequence_of_statements
[exception
exception_handler
{exception_handler}]]
end [package_simple_name];
The simple name at the start of a package body must repeat the package
identifier. Similarly if a simple name appears at the end of the package
specification or body, it must repeat the package identifier.
If a subprogram declaration, a package declaration, a task declaration, or
a generic declaration is a declarative item of a given package
specification, then the body (if there is one) of the program unit declared
by the declarative item must itself be a declarative item of the
declarative part of the body of the given package.
Notes:
A simple form of package, specifying a pool of objects and types, does not
require a package body. One of the possible uses of the sequence of
statements of a package body is to initialize such objects. For each
subprogram declaration there must be a corresponding body (except for a
subprogram written in another language, as explained in section 13.9). If
the body of a program unit is a body stub, then a separately compiled
subunit containing the corresponding proper body is required for the
program unit (see 10.2). A body is not a basic declarative item and so
cannot appear in a package specification.
A package declaration is either a library package (see 10.2) or a
declarative item declared within another program unit.
References: basic declarative item 3.9, body stub 10.2, declarative item
3.9, declarative part 3.9, exception handler 11.2, generic body 12.2,
generic declaration 12.1, identifier 2.3, library unit 10.1, object 3.2,
package body 7.3, program unit 6, proper body 3.9, sequence of statements
5.1, simple name 4.1, subprogram body 6.3, subprogram declaration 6.1,
subunit 10.2, task body 9.1, task declaration 9.1, type 3.3
> 7.2 Package Specifications and Declarations
The first list of declarative items of a package specification is called
the visible part of the package. The optional list of declarative items
after the reserved word private is called the private part of the package.
An entity declared in the private part of a package is not visible outside
the package itself (a name denoting such an entity is only possible within
the package). In contrast, expanded names denoting entities declared in
the visible part can be used even outside the package; furthermore, direct
visibility of such entities can be achieved by means of use clauses (see
4.1.3 and 8.4).
The elaboration of a package declaration consists of the elaboration of its
basic declarative items in the given order.
Notes:
The visible part of a package contains all the information that another
program unit is able to know about the package. A package consisting of
only a package specification (that is, without a package body) can be used
to represent a group of common constants or variables, or a common pool of
objects and types, as in the examples below.
Example of a package describing a group of common variables:
package PLOTTING_DATA is
PEN_UP : BOOLEAN;
CONVERSION_FACTOR,
X_OFFSET, Y_OFFSET,
X_MIN, Y_MIN,
X_MAX, Y_MAX: REAL; -- see 3.5.7
X_VALUE : array (1 .. 500) of REAL;
Y_VALUE : array (1 .. 500) of REAL;
end PLOTTING_DATA;
Example of a package describing a common pool of objects and types:
package WORK_DATA is
type DAY is (MON, TUE, WED, THU, FRI, SAT, SUN);
type HOURS_SPENT is delta 0.25 range 0.0 .. 24.0;
type TIME_TABLE is array (DAY) of HOURS_SPENT;
WORK_HOURS : TIME_TABLE;
NORMAL_HOURS : constant TIME_TABLE :=
(MON .. THU => 8.25, FRI => 7.0, SAT | SUN => 0.0);
end WORK_DATA;
References: basic declarative item 3.9, constant 3.2.1, declarative item
3.9, direct visibility 8.3, elaboration 3.9, expanded name 4.1.3, name 4.1,
number declaration 3.2.2, object declaration 3.2.1, package 7, package
declaration 7.1, package identifier 7.1, package specification 7.1, scope
8.2, simple name 4.1, type declaration 3.3.1, use clause 8.4, variable
3.2.1
> 7.3 Package Bodies
In contrast to the entities declared in the visible part of a package
specification, the entities declared in the package body are only visible
within the package body itself. As a consequence, a package with a package
body can be used for the construction of a group of related subprograms (a
package in the usual sense), in which the logical operations available to
the users are clearly isolated from the internal entities.
For the elaboration of a package body, its declarative part is first
elaborated, and its sequence of statements (if any) is then executed. The
optional exception handlers at the end of a package body service
exceptions raised during the execution of the sequence of statements of the
package body.
Notes:
A variable declared in the body of a package is only visible within this
body and, consequently, its value can only be changed within the package
body. In the absence of local tasks, the value of such a variable remains
unchanged between calls issued from outside the package to subprograms
declared in the visible part. The properties of such a variable are
similar to those of an "own" variable of Algol 60.
The elaboration of the body of a subprogram declared in the visible part of
a package is caused by the elaboration of the body of the package. Hence a
call of such a subprogram by an outside program unit raises the exception
PROGRAM_ERROR if the call takes place before the elaboration of the package
body (see 3.9).
Example of a package:
package RATIONAL_NUMBERS is
type RATIONAL is
record
NUMERATOR : INTEGER;
DENOMINATOR : POSITIVE;
end record;
function EQUAL(X,Y : RATIONAL) return BOOLEAN;
function "/" (X,Y : INTEGER) return RATIONAL; -- to construct a rational number
function "+" (X,Y : RATIONAL) return RATIONAL;
function "-" (X,Y : RATIONAL) return RATIONAL;
function "*" (X,Y : RATIONAL) return RATIONAL;
function "/" (X,Y : RATIONAL) return RATIONAL;
end;
package body RATIONAL_NUMBERS is
procedure SAME_DENOMINATOR (X,Y : in out RATIONAL) is
begin
-- reduces X and Y to the same denominator:
...
end;
function EQUAL(X,Y : RATIONAL) return BOOLEAN is
U,V : RATIONAL;
begin
U